package com.hc.server;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import com.hc.util.CommonData;

public class MapFileUtil {
	public static final int DOWNLOAD_MAP = 1;
	public static final int DOWNLOAD_ALARM_FILE = 2;
	public static final int DOWNLOAD_PATH_FILE = 3;
	public static final String MAP_FILE = "uploadFile";
	public static final String ALARM_FILE = "alarmFile";
	public static final String PATH_FILE = "pathFile";
	public static final String NO_FILE_INFO = "null";
	public static String getMapName(){
		String fileName = "";
		File file = new File(CommonData.systemPath + "config"+File.separator+"uploadFile");
		File[] fs = file.listFiles();
		if(fs.length > 0){
			fileName = fs[0].getName();
		}
		return fileName;
	}//"png", "jpg","JPG","Jpg","GIF","gif"
	
	public static String getMapNameById(String mapId){
		String fileName = "";
		String pFile = CommonData.systemPath + "config"+File.separator+"uploadFile"+File.separator+mapId;
		File file = new File(pFile);
		File[] fs = file.listFiles();
		for(File f : fs){
			if(checkIsImg(pFile + File.separator + f.getName())){
				fileName = f.getName();
			}
		}
		return fileName;
	}
	
	public static boolean checkIsImg(String name){
		try {
			BufferedImage bi = ImageIO.read(new File(name));
			if(bi == null){
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	public static void main(String[] args) {
		try {
			BufferedImage bi = ImageIO.read(new File("E:\\mac.zip"));
			if(bi == null){
				System.out.println(false);
			}else{
				System.out.println(true);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void clearMapFile(String path){
		File file = new File(path);
		File[] fs = file.listFiles();
		for(File f : fs){
			f.delete();
		}
	}
	
	public static void createFile(String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdir();
		}
	}
	
	public static void checkFileExist(String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdir();
		}else{
			clearMapFile(path);
		}
	}
	
	public static void deleteFile(String path){
		File file = new File(path);
		if(file.exists()){
			file.delete();
		}
	}
	
	public static boolean checkFileExist(String path, String fileName){
		File file = new File(path);
		File[] f = file.listFiles();
		if(f.length == 0){
			return false;
		}else{
			for(File fi : f){
				if(fileName.equals(fi.getName())){
					return true;
				}
			}
		}
		return false;
	}
	
	public static String getFileExt(String fileName){
		return fileName.substring(fileName.lastIndexOf(".") + 1);
	}
}

